<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        video {
            width: 500px;
            height: 500px;
            border: 1px solid;
        }
    </style>
</head>

<body>
    <video autoplay></video>
    <script>
        function accessCamera() {
            navigator.mediaDevices.getUserMedia({ video: true })
                .then(function (stream) {
                    // 成功获取媒体流后，将其绑定到一个视频元素上
                    const video = document.querySelector('video');
                    if (video) {
                        video.srcObject = stream;
                    }
                })
                .catch(function (err) {
                    // 处理错误情况
                    console.error('无法访问摄像头', err);
                });
        }

        // 调用函数来请求摄像头权限
        accessCamera();
    </script>
</body>

</html>